package com.echo.blog.business.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.echo.blog.business.blog.dao.BlogTypeMapper;
import com.echo.blog.business.blog.model.BlogType;
import com.echo.blog.business.blog.service.IBlogTypeService;
import com.echo.model.constant.ConstantUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author chentl123
 * @since 2019-09-10
 */
@Service
public class BlogTypeServiceImpl extends ServiceImpl<BlogTypeMapper, BlogType> implements IBlogTypeService {
    @Override
    public List<Map<String, Object>> getBloyTypeList() {
        QueryWrapper<BlogType> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        List<Map<String, Object>> list = this.listMaps(wrapper);
        return list;
    }

    @Override
    public BlogType getBlogTypeIndex(Integer userId, Integer blogType) {
        BlogType indexData = this.baseMapper.getBlogTypeIndex(userId, blogType);
        return indexData;
    }

    @Override
    public List<BlogType> getBlogTypeList(Integer userId) {
        QueryWrapper<BlogType> wrapper = new QueryWrapper<>();
        wrapper.eq(ConstantUtils.Public.CREATEBY, userId);
        List<BlogType> list = this.list(wrapper);
        return list;
    }

    @Override
    public Integer selectMaxId() {
        return this.baseMapper.selectMaxId();
    }

    @Override
    public boolean insertBlogType(String sortName, Integer createby) {
        Integer maxId = selectMaxId();
        maxId = maxId == null ? 0 : maxId;
        BlogType blogType = new BlogType();
        blogType.setCreateby(createby);
        blogType.setUpdateby(createby);
//        blogType.setBlogType(maxId + 1);
        blogType.setIcon("mdi-pill");
        blogType.setName(sortName);
        this.save(blogType);
        return true;
    }

    @Override
    public boolean validRepeat(String name, Integer userId) {
        QueryWrapper<BlogType> wrapper = new QueryWrapper<>();
        wrapper.eq(ConstantUtils.Public.CREATEBY, userId)
                .eq(ConstantUtils.Public.NAME, name);
        Integer count = this.count(wrapper);
        return count > 0;
    }
}
